package com.maserb.monitor.service.schoolConduct.impl;

import com.maserb.monitor.common.CreateTimeUtils;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolConduct.SchoolConductItemEntity;
import com.maserb.monitor.repository.schoolConduct.SchoolConductItemRepository;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.schoolConduct.SchoolConductItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolConductItemServiceImpl extends JpaSpecificationServiceImpl<SchoolConductItemRepository,
        SchoolConductItemRepository, SchoolConductItemEntity, String>
        implements SchoolConductItemService {

    @Autowired
    JwOrgService jwOrgService;

    @Override
    public Page<SchoolConductItemEntity> findAll(Pageable pageable ,String name ,String schoolId) {
        return this.findAll(getSpecification(name,schoolId), pageable);
    }

    @Override
    public List<SchoolConductItemEntity> findAll(String name , String schoolId) {
        return new ArrayList(this.findAll(getSpecification(name,schoolId),new PageRequest(0,1000)).getContent());
    }

    @Override
    public List<SchoolConductItemEntity> findBySchoolId(String schoolId) {
        return this.repository.findBySchoolId(schoolId);
    }

    @Override
    public void saveImport(List<SchoolConductItemEntity> list,String schoolId) {
        for(SchoolConductItemEntity entity:list){
            entity.setCreateTime(CreateTimeUtils.getCreateTime());
            if(!StringUtils.isEmptyOrWhitespace(schoolId)){
                JwOrgEntity orgEntity = jwOrgService.findOne(schoolId);
                if(null!=orgEntity){
                    entity.setSchoolId(schoolId);
                    entity.setSchoolName(orgEntity.getName());
                }
            }
           this.save(entity);
        }
    }

    private Specification<SchoolConductItemEntity> getSpecification(String name ,String schoolId){
        return new Specification<SchoolConductItemEntity>() {
            @Override
            public Predicate toPredicate(Root<SchoolConductItemEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathName = root.get("name");
                Path<String> pathSchoolId = root.get("schoolId");
                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(name)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathName, "%"+name+"%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(schoolId)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathSchoolId, schoolId)));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }



}